﻿using OPCPlatformCore.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OPCPlatformCore.Models.OPCUaModels
{
    public class OPCUaMachine
    {
        /// <summary>
      /// 特别注意,此属性应该全局唯一
      /// </summary>
        public string MachineName { get; private set; }
        public List<OPCUaTagInfo> TagInfos { get; private set; }
        public OPCUaMachine(string machineName)
        {
            if (string.IsNullOrEmpty(machineName))
            {
                throw new OPCMachineDefineException();
            }
            MachineName = machineName;
            TagInfos = new List<OPCUaTagInfo>();
        }
        public void AddTag(string tagName, string address)
        {
            //FriendlyName必须要求在一个Group中是唯一的
            if (TagInfos.Any(t => t.FriendlyName == tagName))
            {
                throw new TagNameRepeatException(tagName);
            }
            OPCUaTagInfo tagInfo = new OPCUaTagInfo(tagName, address);
            TagInfos.Add(tagInfo);
        }
        public void AddTag(List<OPCUaTagInfo> tagInfos)
        {
            //此处仅使用直接替换的方式,主要是为了当导入配置的时候不需要一个个Tag进行添加
            TagInfos = tagInfos;
        }
    }
}
